﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace ManageBanking.Models
{
    public class ManageUserModels
    {
        private OnlineBankingDataContext olbk = new OnlineBankingDataContext();

        public static string EncryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            // Buoc 1: Bam chuoi su dung MD5
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
            // Step 2. Tao doi tuong TripleDESCryptoServiceProvider moi
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            // Step 3. Cai dat bo ma hoa
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            // Step 4. Convert chuoi (Message) thanh dang byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            // Step 5. Ma hoa chuoi
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                // Xoa moi thong tin ve Triple DES va HashProvider de dam bao an toan
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            // Step 6. Tra ve chuoi da ma hoa bang thuat toan Base64
            return Convert.ToBase64String(Results);
        }




        public bool ChangeSecPass(string pass,Username us)
        {
            bool kq = true;
            try
            {
                LoginModels lg = new LoginModels();

                string decpass = lg.DecryptString(us.Password, us.SecondaryPass);
                string encpass = getMD5(pass);
                string decpass1 = EncryptString(decpass, encpass);
                Username us1 = olbk.Usernames.Where(a => a.IdUsername == us.IdUsername).First();
                us1.Password = decpass1;
                us1.SecondaryPass = encpass;
                olbk.SubmitChanges();
            }
            catch
            {
                kq = false;
            }
        return kq;
        
        }
        public bool ChangeSecPassAdmin(string pass,ManageBanking.Areas.Admin.Models.Username us)
        {
            bool kq = true;
            try
            {
                LoginModels lg = new LoginModels();

                string decpass = lg.DecryptString(us.Password, us.SecondaryPass);
                string encpass = getMD5(pass);
                string decpass1 = EncryptString(decpass, encpass);
                Username us1 = olbk.Usernames.Where(a => a.IdUsername == us.IdUsername).First();
                us1.Password = decpass1;
                us1.SecondaryPass = encpass;
                olbk.SubmitChanges();
            }
            catch
            {
                kq = false;
            }
            return kq;

        }

        public bool ChangeInformation(string username, string name, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string identificard, string SecPass, int idadmin, int iduser)
        {

            LoginModels lg = new LoginModels();
            bool kq = true;
            try
            {
                Username ac = olbk.Usernames.Where(a => a.IdUsername==iduser).First();
                ac.Username1 = username;
                ac.Name = name;
                ac.Sex = sex;
                ac.DateBirth = birth;
                ac.Address = address;
                ac.Email = Email;
                ac.PlaceBirth = PlaceBirth;
                ac.Job = job;
                ac.IdentityCard = identificard;
                if (SecPass != "")
                {
                    string pass = lg.DecryptString(ac.Password, ac.SecondaryPass);
                    string secpass2 = getMD5(SecPass);
                    string pass1 = EncryptString(pass, secpass2);
                    ac.SecondaryPass = secpass2;
                    ac.Password = pass1;
                
                
                }
                ac.Idadmin = idadmin;
                olbk.SubmitChanges();
            }
            catch
            {
                kq = false;
            }
            return kq;
        }


        public bool AnswerIdentity1(string username, string answer)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username.Trim()).First();
            if (ac.IdentityCard == answer.Trim())
            {

                return true;
            }
            else
                return false;
        }
        public bool AnswerIdentity(string identity)
        {
            int count = olbk.Usernames.Where(a => a.IdentityCard == identity.Trim()).Count();
            if (count>= 1)
            {

                return true;
            }
            else
                return false;
        }
        public bool CheckUSer(string answer)
        {
            int cout = olbk.Usernames.Where(a => a.Username1 == answer.Trim()).Count();
            if (cout >=1)
                return true;
            else
                return false;



        }
        public bool CheckUSer2(string answer)
        {
            int cout = olbk.Usernames.Where(a => a.Username1 == answer.Trim() && a.IdTypeUser==2).Count();
            if (cout >= 1)
                return true;
            else
                return false;



        }

        public bool AnswerEmail(string username, string answer)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username.Trim()).First();
            if (ac.Email == answer.Trim())
            {

                return true;
            }
            else
                return false;
        }
        public bool Answer_Email(string Email)
        {
            int ac = olbk.Usernames.Where(a => a.Email == Email.Trim()).Count();
            if (ac >= 1)
            {
                return true;
            }
            else
                return false;
        }
        public bool AnswerSecondary(string username, string answer)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username.Trim()).First();
            if (ac.SecondaryPass == answer.Trim())
            {

                return true;
            }
            else
                return false;
        }
        public bool CheckLock(string username)
        {
            bool kq = true;
            int ac = olbk.Usernames.Where(a => a.Username1 == username.Trim() && a.Lock == true).Count();
            if (ac >= 1)
                kq = true;
            else
                kq = false;
            return kq;

        }

        public void ChangePassword(string idaccount, string newpassword)
        {
            Username ac = olbk.Usernames.Where(a => a.IdUsername.Equals(idaccount.Trim())).First();
            ac.Password = EncryptString(newpassword.Trim(), ac.SecondaryPass);
            olbk.SubmitChanges();
        }
        public bool CheckOldPassword(string idaccount, string oldpassword)
        {
            var ac = olbk.Usernames.Where(ac1 => ac1.IdUsername.Equals(idaccount.Trim())).First();
            if (ac.Password.Equals(EncryptString(oldpassword.Trim(), ac.SecondaryPass)))
                return true;
            return false;
        }
        public string getMD5(String str)
        {
            MD5CryptoServiceProvider mdcsp = new MD5CryptoServiceProvider();
            byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
            b = mdcsp.ComputeHash(b);
            StringBuilder s = new StringBuilder();
            foreach (byte by in b)
            {
                s.Append(by.ToString("x2").ToLower());
            }
            return s.ToString();
        }
        public Username TakeUser(int id)
        {
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            return olbk.Usernames.Where(a => a.IdUsername == id).First();
        }
        
        



    }
}
